app.controller('ReportIncome', function($scope, $http, common){
	$scope.dateS 	= '';
	$scope.dateE 	= '';

	//初始化JS
	$scope.init_js 	= function(){
 		$('.datepicker').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 2
	    });

	    //回车键监听
	    common.listenEnterKey(function(){
	        $scope.load_data();
	    });
	}
	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/Report/getIncome?'+
			'&date_start='+$scope.dateS+
			'&date_end='+$scope.dateE;

		$http.get(url).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			var data 	= info.data;
			var xAxis 	= new Array();
			var dataA 	= new Array();
			var dataB 	= new Array();
			var dataC 	= new Array();
			for(var k in data){
				xAxis.push(data[k].date);
				dataA.push(parseInt(data[k].income));
				dataB.push(parseInt(data[k].deduct));
				dataC.push(parseInt(data[k].profit));
			}
			var series 	= [
				{name : '收入金额', data : dataA},
				{name : '分红金额', data : dataB},
				{name : '净利润金额', data : dataC}
			];
			console.log(series);
			$scope.make_chart($('.box-chart'), '平台收入统计', xAxis, '金额(元)', series);
		})
	}

	$scope.make_chart 	= function(dom, title, xAxis, yTitle, series){
	   	var json = {};
	   	json.title = title = {
	      	text: '<b>'+title+'</b>'   
	   	};
	   	json.subtitle = subtitle = {
	      	text: false
	   	}
	   	json.xAxis = {
	      	categories: xAxis
	   	};
	   	json.yAxis = {
	      	title: {
	         	text: yTitle
	      	},
	      	plotLines: [{
	         	value: 0,
	         	width: 1,
	         	color: '#808080'
	      	}]
	   	};   
	   	json.tooltip = {
	      	valueSuffix: ''
	   	}
	   	json.legend = {
	      	// layout: 'vertical',
	      	// align: 'right',
	      	// verticalAlign: 'middle',
	      	// borderWidth: 0
	   	};
	   	json.series = series;

	   	$(dom).highcharts(json);
 	}

	$scope.load_data();
	$scope.init_js();
});